package com.java110.mall.openapi.cmd.token;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.store.StoreDto;
import com.java110.dto.user.StoreStaffDto;
import com.java110.intf.user.IStoreStaffV1InnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "token.getSsoToken")
public class GetSsoTokenCmd extends Cmd {
    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;


    @Autowired
    private IStoreStaffV1InnerServiceSMO storeStaffV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "tel", "未包含手机号");
        Assert.hasKeyAndValue(reqJson, "storeName", "未包含物业公司名称");
        Assert.hasKeyAndValue(reqJson, "storeId", "未包含物业公司名称");

        String storeId = CmdContextUtils.getStoreId(context);

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(storeId);
        storeDto.setStoreTypeCd(StoreDto.STORE_TYPE_ADMIN);
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);

        if (ListUtil.isNull(storeDtos)) {
            throw new CmdException("登陆账户没有权限");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(reqJson.getString("storeId"));
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);
        JSONObject paramOut = new JSONObject();

        if (ListUtil.isNull(storeDtos)) {
            String code = GenerateCodeFactory.getUUID();
            CommonCache.setValue("getSSoToken_" + code, reqJson.toJSONString(), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
            paramOut.put("url", "/user.html#/pages/frame/wuyeRegister?code=" + code);
            context.setResponseEntity(ResultVo.createResponseEntity(paramOut));
            return;
        }


        StoreStaffDto storeStaffDto = new StoreStaffDto();
        storeStaffDto.setTel(reqJson.getString("tel"));
        storeStaffDto.setStoreId(reqJson.getString("storeId"));
        List<StoreStaffDto> storeStaffDtos = storeStaffV1InnerServiceSMOImpl.queryStoreStaffs(storeStaffDto);

        if (ListUtil.isNull(storeStaffDtos)) {
            throw new CmdException("商城系统员工不存在，请联系管理员！");
        }


        String token = GenerateCodeFactory.getUUID();

        CommonCache.setValue("sso_" + token, JSONObject.toJSONString(storeStaffDtos.get(0)), CommonCache.SSO_TOKEN_EXPIRE_TIME);

        paramOut = new JSONObject();
        paramOut.put("url", "/sso.html#/pages/frame/sso?token=" + token);

        context.setResponseEntity(ResultVo.createResponseEntity(paramOut));


    }
}
